package com.dl7.mvp.base.baseadapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<T> mDatas = new ArrayList<>();
    private OnItemClickListener mListener;

    private ArrayList<View> mHeaderViews = new ArrayList<>();
    private ArrayList<View> mFootViews = new ArrayList<>();

    protected Context mContext;
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return (isHeader(position) || isFooter(position))
                            ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp != null
                && lp instanceof StaggeredGridLayoutManager.LayoutParams
                && (isHeader(holder.getLayoutPosition()) || isFooter(holder.getLayoutPosition()))) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(true);
        }
    }

    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }

    final public void addHeader(final View view) {
        if (view != null && mHeaderViews.contains(view)) {
            mHeaderViews.remove(view);
        }
        mHeaderViews.add(view);
        notifyItemInserted(mHeaderViews.indexOf(view));
    }

    final public void removeHeader(View view) {
        if (null == view || !mHeaderViews.contains(view)) {
            return;
        }
        int index = mHeaderViews.indexOf(view);
        mHeaderViews.remove(view);
        notifyItemRemoved(index);
    }

    public void addFooter(final View view) {
        if (view != null && mFootViews.contains(view)) {
            mFootViews.remove(view);
        }
        mFootViews.add(view);
        notifyItemInserted(mFootViews.indexOf(view));
    }

    final public void removeFooter(View view) {
        if (null == view || !mFootViews.contains(view)) {
            return;
        }
        int index = mFootViews.indexOf(view);
        mFootViews.remove(view);
        notifyItemRemoved(index);
    }


    public void addDatas(ArrayList<T> datas) {
        if (datas != null) {
            mDatas.addAll(datas);
            notifyDataSetChanged();
        }
    }

    public boolean isHeader(int position) {
        return position >= 0 && position < mHeaderViews.size();
    }

    public boolean isFooter(int position) {
        return position < getItemCount() && position >= getItemCount() - mFootViews.size();
    }

    public int getHeadersCount() {
        return mHeaderViews.size();
    }

    public int getFootersCount() {
        return mFootViews.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        //处理头部
        View headerView = getViewByHashCodeFromList(mHeaderViews, viewType);
        if (headerView != null) {
            return new ViewHolder(headerView);
        }
        //处理底部
        View footView = getViewByHashCodeFromList(mFootViews, viewType);
        if (footView != null) {
            return new ViewHolder(footView);
        }
        return onCreate(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (isHeader(position)) {
            return;
        }
        if (isFooter(position)) {
            return;
        }
        final int pos = position - getHeadersCount();
        final T data = mDatas.get(pos);

        onBind(holder, pos, data);

        if(mListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(pos, data);
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return getHeadersCount() + getFootersCount() + mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeader(position)) {
            return mHeaderViews.get(position).hashCode();
        }
        if (isFooter(position)) {
            return mFootViews.get(position - (getItemCount() - mFootViews.size())).hashCode();
        }
        return 0;
    }

//    @Override
//    public long getItemId(int position) {
//        com.orhanobut.logger.Logger.i("posi: " + position);
//        if (mDatas != null && position >= getHeadersCount()) {
//            int adjPosition = position - getHeadersCount();
//            int dataCount = mDatas.size();
//            if (adjPosition < dataCount) {
//                return adjPosition;
//            }
//        }
//        return -1;
//    }

    private View getViewByHashCodeFromList(ArrayList<View> views, int hashCode) {
        if (null == views) {
            return null;
        }
        for (View v : views) {
            if (v.hashCode() == hashCode) {
                return v;
            }
        }
        return null;
    }

    public abstract RecyclerView.ViewHolder onCreate(ViewGroup parent, final int viewType);

    public abstract void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, T data);

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface OnItemClickListener<T> {
        void onItemClick(int position, T data);
    }
}
